package com.icim.rest;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.icim.common.LoadHistoryConfig;
import com.icim.rabbitmq.util.RabbitmqManager;
import com.icim.tx.ShowStatsPo.ShowStatusPoI;
import com.icim.tx.ShowStatsPo.ShowStatusPoO;
import com.icim.tx.ShowStatsPo.ShowStatusPoOA;
import com.icim.util.JackUtil;
import com.mysql.jdbc.Util;
import com.rabbitmq.client.AMQP.Queue.DeclareOk;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.ShutdownSignalException;

@Controller
public class ShowStatusController {

	private static Logger logger = Logger.getLogger(LoadHistoryMainController.class);


	@Resource
	CachingConnectionFactory rabbitConnFactory;
	@Resource
	private LoadHistoryConfig loadHistoryConfig;
	
	private void returnResponse(HttpServletResponse response,ShowStatusPoO outTrx) throws Exception{
		response.reset();
		response.setContentType("application/json");
		response.setHeader("Content-type", "text/html;charset=UTF-8");  //支持中文
		response.addHeader("Pragma", "no-cache");
		response.addHeader("Cache-Control", "no-cache");
		
		PrintWriter pw = response.getWriter();
		pw.print(JackUtil.toJsonString(outTrx));
		pw.flush();
	}
	@RequestMapping(value = "/showStatus.do")
	@ResponseBody
	public void showStatus(HttpServletRequest request, HttpServletResponse response, String inObj)
			throws Exception {
		ShowStatusPoO outTrx = new ShowStatusPoO();
		String queues = loadHistoryConfig.getQueues();
		try {
			if(StringUtils.isEmpty(queues)){
				outTrx.setRtn_code("0000001");
				throw new Exception("配置档中参数[queues]定义队列为空,请重新配置后重启loadhis");
			}
			String[] queueList = queues.split(",");
//			ShowStatusPoI inTrx = new ShowStatusPoI();
//			inTrx = JackUtil.fromJson(inObj, inTrx);
//			int tblCnt = inTrx.getTbl_cnt();
			Connection conn = rabbitConnFactory.createConnection();
			Channel channel = conn.createChannel(false);

			List<ShowStatusPoOA> oaryList = new ArrayList<ShowStatusPoOA>();
//			for (int i = 0; i < tblCnt; i++) {
			for(String queueName : queueList){
//				String queueName = inTrx.getQueueList().get(i);
				DeclareOk dOk = channel.queueDeclarePassive(queueName);
				int depth = dOk.getMessageCount();
				ShowStatusPoOA oary = new ShowStatusPoOA();
				oary.setQueueName(queueName);
				oary.setDepth(depth);
				oaryList.add(oary);
			}
			conn.close();

			outTrx.setRtn_code("0000000");
			outTrx.setRtn_mesg("SUCCESS");
			outTrx.setTbl_cnt(oaryList.size());
			outTrx.setOary(oaryList);
			
		} catch (Exception e) {
			logger.error(Util.stackTraceToString(e));
			outTrx.setRtn_code("1");
			outTrx.setRtn_mesg(Util.stackTraceToString(e));
		}finally{
			returnResponse(response,outTrx);
		}
	}

}
